﻿/****************
* Authored by Zhuo "Wizcas" Chen
* 2018 © Wubi.fun all rights reserved
*****************/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Timer : MonoBehaviour
{
    [SerializeField] Text _time;

    public void Show()
    {
        if (!Game.instance.isTimeChallenge) Hide();
        else gameObject.SetActive(true);
    }

    public void Hide()
    {
        gameObject.SetActive(false);
    }

    void UpdateTime()
    {
        var secs = Game.instance.timeRemaining;
        var timeText = secs >= 0 ? string.Format("{0}m{1}s", (int)(secs / 60), Mathf.CeilToInt(secs % 60)) : "Time's up";
        _time.text = timeText;
        _time.color = secs < 0 ? Color.red : Color.white;
    }

    private void Update()
    {
        UpdateTime();
    }
}
